﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.NRBZ;
using Rison.BusinessFacade.Facade.NRBZ;
using Rison.DataAccess.Common;

namespace Rison.Web.NRBZ
{
    public partial class HdqdDetail : NrfbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void LoadInformation(DataRow dr)
        {
            if (dr != null)
            {

                string _ldxjNum = dr["ldxjNum"].ToString();
                SetTextBoxValue(_ldxjNum, "ldxjNum");

                string _zxtsxjNum = dr["zxtsxjNum"].ToString();
                SetTextBoxValue(_zxtsxjNum, "txtzxtsxjNum");

                string _dlxjNum = dr["dlxjNum"].ToString();
                SetTextBoxValue(_dlxjNum, "dlxjNum");
                this.onlinePercent.Text = dr["onlinePercent"].ToString().Trim();
                this.interactedNum.Text = dr["interactedNum"].ToString().Trim();
                this.planInteractedNum.Text = dr["planInteractedNum"].ToString().Trim();

                //this.rblIfkzzfhd.SelectedValue = dr["Ifkzzfhd"].ToString() == "是" ? "是" : "否";

                //htmlTable
                LoadHtmlTable(NrbzSubType.NrbzSubType41.ToString(), "HdqdDetail1_GZRY1", "tableGZRY1", 2);
                LoadHtmlTable(NrbzSubType.NrbzSubType42.ToString(), "HdqdDetail1_GZRY2", "tableGZRY2", 2);
            }
        }

        public override long SaveInformation()
        {
            NrbzFacade facade = new NrbzFacade();
            List<DataField> l = new List<DataField>();

            int returnValue = 0;

            l.Add(new DataField("ldxjNum", GetLdxjNum()));
            l.Add(new DataField("zxtsxjNum", GetTextBoxValue("txtzxtsxjNum", 5)));
            l.Add(new DataField("dlxjNum", GetTextBoxValue("dlxjNum", 3)));
            l.Add(new DataField("onlinePercent",this.onlinePercent.Text.Trim()));

            //l.Add(new DataField("Ifkzzfhd", rblIfkzzfhd.SelectedValue));
            l.Add(new DataField("interactedNum",this.interactedNum.Text.Trim()));
            l.Add(new DataField("planInteractedNum",this.planInteractedNum.Text.Trim()));
            returnValue = facade.updateValue(l, NrbzId);

            //htmlTable
            string returnSubValue = "";
            returnSubValue = SaveHtmlTabel("HdqdDetail1_GZRY1", NrbzSubType.NrbzSubType41.ToString(), 2);
            returnSubValue = SaveHtmlTabel("HdqdDetail1_GZRY2", NrbzSubType.NrbzSubType42.ToString(), 2);

            return returnValue;
        }
        protected string GetLdxjNum()
        {
            string t2 = this.ldxjNum2.Text;
            int t4 = 0;
            try
            {
                t4 = int.Parse(this.ldxjNum4.Text);
            }
            catch (Exception e)
            {
                t4 = 0;
            }
            int t3 = 0;
            try
            {
                t3 = int.Parse(this.ldxjNum3.Text);
            }
            catch (Exception e)
            {
                t3 = 0;
            }
            string t5 = this.ldxjNum5.Text;
            string t6 = this.ldxjNum6.Text;

            int t1 = t3 + t4;
            return String.Format("{0},{1},{2},{3},{4},{5}", t1, t2, t3, t4, t5, t6);
        }
        #region "textbox&radionbox"
        protected void SetTextBoxValue(string value, string field)
        {
            string[] s = value.Split(',');
            for (int i = 0; i < s.Length; i++)
            {
                Control o =this.FindControl(field + (i + 1).ToString());

                if (o.GetType().Name == "TextBox")
                {
                    ((TextBox)o).Text = s[i].ToString();
                }
                else if (o.GetType().Name == "RadioButtonList")
                {
                    ((RadioButtonList)o).SelectedValue = s[i].ToString() == "是" ? "是" : "否";
                }                
            }
        }

        protected string GetTextBoxValue(string field, int num)
        {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
                Control o = this.FindControl(field + (i + 1).ToString());
                string value = "";

                if (o.GetType().Name == "TextBox")
                {
                    value = ((TextBox)o).Text;
                }
                else if (o.GetType().Name == "RadioButtonList")
                {
                    value = ((RadioButtonList)o).SelectedValue;
                }       

                if (i == num - 1)
                {
                    s.AppendFormat("{0}", value);
                }
                else
                {
                    s.AppendFormat("{0},",value);
                }
            }
            return s.ToString();
        }
        #endregion

        #region "子网站 create/delete/update htmlTable"
        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="field">ZfgkDetail1_input_5_1</param>
        /// <returns></returns>
        private string SaveHtmlTabel(string field, string Type, int fieldNum)
        {
            string info = HttpContext.Current.Request.Form[field];
            NrbzFacade facade = new NrbzFacade();

            if (!string.IsNullOrEmpty(info))
            {               
                string msg = facade.SaveSubInfobyParentId(NrbzId, Type, fieldNum, info);
                return msg;
            }
            else
            {
                int returnVal = facade.DeleteSubInfobyParentId(NrbzId, Type);
                if (returnVal < 0) return "删除有误！";       
            }
            return "";

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="tablename">ZfgkDetail1_table5_1</param>
        /// <param name="fieldname">ZfgkDetail1_input_5_1</param>
        private void LoadHtmlTable(string Type, string fieldname, string tablename, int fieldNum)
        {
            NrbzFacade facade = new NrbzFacade();
            DataTable dt = facade.LoadSubInfobyParentId(NrbzId, Type, fieldNum);
            if (dt == null || dt.Rows.Count == 0)
            {
                DrawOneRow(tablename, fieldname, "",""); //blank row                
            }
            else
            {

                foreach (DataRow dr in dt.Rows)
                {
                    DrawOneRow(tablename, fieldname, dr[0].ToString(),dr[1].ToString());
                }

            }
        }

        private void DrawOneRow(string t, string inputname, string p1, string p2)
        {
            if (t == "tableGZRY1")
            {
                DrawGZRY1OneRow(t, inputname, p1, p2);
            }
            else
            {
                DrawGZRY2OneRow(t, inputname, p1, p2);
            }

        }

        private void DrawGZRY1OneRow(string t, string inputname, string p1,string p2)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "活动名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:300px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            if (p2 == "") p2 = "http://";
            ce.InnerHtml = "链接地址 <input type='text' name='" + inputname + "' style='width:200px' class='Text' style='width:200px;' value='" + p2 + "'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }

        private void DrawGZRY2OneRow(string t, string inputname, string p1,string p2)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "活动名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:300px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "计划日期 <input class='Wdate' type='text' onClick='WdatePicker()' name='" + inputname + "' value='" + p2 + "' />";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }
        #endregion
        public string departid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["DepartID"]) ? "" : Request.QueryString["DepartID"];
            }
        }
    }
}